package com.jlboot.core.util;

import com.jfinal.kit.JsonKit;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @author wangsanfei
 * @version 1.0
 * Create by 2023/12/29 20:45
 */
@Slf4j
public class HttpUtil {

    private static OkHttpClient okHttpClient = new OkHttpClient();

    public static boolean isAjax(HttpServletRequest request){
        return  (request.getHeader("X-Requested-With") != null
        && "XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) ;
    }

    public static String post(String url, Map<String,String> queryParams, Map<String,String> body){

        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if(queryParams!=null){
            for (String s : queryParams.keySet()) {
                String v=queryParams.get(s);
                urlBuilder.addQueryParameter(s,v);
            }
        }

        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        for (String s : body.keySet()) {
            String v=body.get(s);
            formBodyBuilder.add(s,v);
        }

        Request request = new Request.Builder()
                .url(urlBuilder.build())
                .post(formBodyBuilder.build())
                .build();

        try {
            Response rep=okHttpClient.newCall(request).execute();
            String repBody=rep.body().string();
            log.info("url:{},rep:{}",url,repBody);
            return repBody;
        } catch (IOException e) {
            log.info("url:{}",url);
            throw new RuntimeException(e);
        }
    }

    public static String postJson(String url, Map<String,String> queryParams, Map<String,Object> body){

        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if(queryParams!=null){
            for (String s : queryParams.keySet()) {
                String v=queryParams.get(s);
                urlBuilder.addQueryParameter(s,v);
            }
        }

        Request request = new Request.Builder()
                .url(urlBuilder.build())
                .post(FormBody.create(MediaType.parse("application/json; charset=utf-8"), JsonKit.toJson(body)))
                .build();

        try {
            Response rep=okHttpClient.newCall(request).execute();
            String repBody=rep.body().string();
            log.info("url:{},rep:{}",url,repBody);
            return repBody;
        } catch (IOException e) {
            log.info("url:{}",url);
            throw new RuntimeException(e);
        }

    }

    public static String get(String url, Map<String,String> queryParams){

        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();

        for (String s : queryParams.keySet()) {
            String v=queryParams.get(s);
            urlBuilder.addQueryParameter(s,v);
        }

        urlBuilder.addQueryParameter("param1", "value1");
        urlBuilder.addQueryParameter("param2", "value2");


        Request request = new Request.Builder()
                .url(urlBuilder.build())
                .get()
                .build();

        try {
            Response rep=okHttpClient.newCall(request).execute();
            String repBody=rep.body().string();
            log.info("url:{},rep:{}",url,repBody);
            return repBody;
        } catch (IOException e) {
            log.info("url:{}",url);
            throw new RuntimeException(e);
        }
    }
    
}
